clear all
set more off
version 14.1
capture log close

//  #1
//  bring-in data
	use ubridge_water.dta, clear
	
	merge 1:1 DataSetID using villages_with_election_returns.dta
	keep if _m==3
	drop _merge
	
	merge 1:1 DataSetID using villages_with_voting_effectiveness.dta, keepus (LC5chair_turnout_registered LC5chair_turnout_adultpop)
	keep if _m==3
	drop _merge	
	
	merge m:1 cluster_id using ubridge_health_long.dta, keepus (healthtype)
	keep if _m==3
	drop _merge
	
//  #2
//  clean data
	
* first drop variables that have all missing values
findname, all(missing(@))

* second, drop clearly unnecessary variables	
drop matchedmeeting

move HC_dist num_meetings
	replace HC_dist=HC_dist/1000
	lab var HC_dist "Distance to health center (km)" 
	
move Arua_dist num_meetings 
	replace Arua_dist=Arua_dist/1000
	lab var Arua_dist "Distance to Arua (km)" 

	gen ldist=log(Arua_dist)
	lab var ldist "Log distance to Arua" 
	su Arua_dist ldist, de

renvars *_sc,   subst(_sc )
drop age_lc1  
renvars *_lc1,  subst(_lc1 ) 	

ren sc_frac fractionalization
	lab var fractionalization "Religious fractionalization"
ren sc_pol polarization

gen lpop=log(adult_pop)
lab var lpop "Log adult population"

******************************
//  #3
* Define control
******************************
foreach y in lpop age poverty_census lugbara_share HHI_Ethnicity polarization HHI_Religion literacy education employed_share Nonagriculture_share Arua_dist{
	 egen `y'_median=median(`y')
	 gen `y'_miss = `y'==.
	 tab `y'_miss
	 replace `y' = `y'_median if `y'==.
	 center `y' , standardize
	 drop `y'_median
}	

gl controls c.c_age##treat c.c_lugbara_share##treat c.c_literacy##treat c.c_Arua_dist##treat 
	su $controls
	
******************************
//  #4
* run regressions
******************************	
	gen WARDI_share=(LC5chair_votes_WADR/LC5chair_valid_votes )
	gen SAKA_share=(LC5chair_votes_SAKA/LC5chair_valid_votes )
	gen turnout = LC5chair_valid_votes/registered_voters
	gen mov=WARDI_share-SAKA_share
	
	foreach y in WARDI_share SAKA_share mov turnout {
	eststo `y'1: reg `y' i.treat healthtype, cl(cluster_id)
	estadd local control ""
	eststo `y'2: reg `y' i.treat healthtype $controls ,  cl(cluster_id) 
	estadd local control "X"
	}

***************************************
* TABLE 19 in Supplementary Information
***************************************

esttab WARDI_share1 WARDI_share2 mov1 mov2 turnout1 turnout2, star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	
clear
exit